﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace kb_24_the_game
{
    class Level3
    {
        #region Variabelen Declaratie
        private Biertje biertje;
        private ContentManager content;
        private Backgrounds scrollBackground1;
        private Player speler;
        private ObstakelBal discobal1, discobal2;
        private List<ObstakelBal> discobalLijst;
        private List<ObstakelTest> obstakelLijst;
        private List<ObstakelTest> lampLijst, speakerLijst;
        private IList<ISprite> allSprites;
        private SpriteFont font;
        private bool bovencollision, up = false; 
        private Rectangle spelerRect;
        private KeyboardState state;

        private XML_IO xml;
        #endregion Variabelen Declaratie

        public Level3(ContentManager content)
        {
            this.content = content;
            allSprites = new List<ISprite>();
            obstakelLijst = new List<ObstakelTest>();
            lampLijst = new List<ObstakelTest>();
            speakerLijst = new List<ObstakelTest>();
            discobalLijst = new List<ObstakelBal>();
            speler = new Player(content, new Vector2(50, 425));
            discobal1 = new ObstakelBal(content, new Vector2(700, 470), "Level3Textures/DiscoBall");
            discobal2 = new ObstakelBal(content, new Vector2(2500, 470), "Level3Textures/DiscoBall");
            biertje = new Biertje(content, new Vector2(150, 400), 3);
            font = content.Load<SpriteFont>("AlgemeneTextures/tussenSchermTekst");
            xml = new XML_IO();
            obstakelLijst = xml.readXML(@"XML/level3.xml");
            foreach (ObstakelTest obstakel in obstakelLijst)
            {
                obstakel.content = content;
            }
            Load();
        }

        private void Load()
        {
            scrollBackground1 = new Backgrounds(content.Load<Texture2D>("Achtergronden/BackgroundLevel03"), new Rectangle(0, 0, 3500, 600));
            #region allSprites.Add
            allSprites.Add(scrollBackground1);
            foreach (ObstakelTest obstakel in obstakelLijst)
            {
                allSprites.Add(obstakel);
            }
            allSprites.Add(discobal1);
            allSprites.Add(discobal2);
            allSprites.Add(biertje);
            allSprites.Add(speler);
            #endregion Variabelen Declaratie

            #region lampLijst.Add
            foreach (ObstakelTest o in obstakelLijst)
            {
                if (o.type == 7)
                {
                    lampLijst.Add(o);
                }
            }
            #endregion lampLijst.Add
            #region speakerLijst.Add
            foreach (ObstakelTest o in obstakelLijst)
            {
                if (o.type == 8)
                {
                    speakerLijst.Add(o);
                }
            }
            #endregion speakerlijst.Add
            #region discobalLijst.Add
            discobalLijst.Add(discobal1);
            discobalLijst.Add(discobal2);
            #endregion discobalLijst.Add

            
        }
        public bool Update(GameTime gameTime)
        {
            spelerRect = new Rectangle((int)speler.playerPosition.X, (int)speler.playerPosition.Y, speler.texture.Width / 4, speler.texture.Height / 2);
            CheckKeys();
            speler.BovenCollision(bovencollision);
            CheckCollisionsMetSpeaker(spelerRect);           
            speler.Springen(up);
            CheckCollisionsMetGarderobe(spelerRect);
            CheckCollsionsSpelerMetLamp(spelerRect);
            CheckCollisionsMetBiertjes(spelerRect);
            CheckColisionDiscobalTrap();
            CheckCollsionsSpelerMetDiscobal(spelerRect);
            biertje.CheckPositionsSpelerBiertje(speler.playerPosition);
            speler.Update(gameTime);
            foreach (ObstakelBal b in discobalLijst)
            {

                b.Update(gameTime);
                b.SetSpelerDirectie(speler.loopDirectie);
            }
            foreach (ObstakelTest l in lampLijst)
            {
                l.ValObstakel(scrollBackground1.rectangle.X);
            }
            foreach (ObstakelTest obstakel in obstakelLijst)
            {
                obstakel.Update(gameTime);
            }
            return CheckEindCondities();
        }

        private void CheckKeys()
        {
            state = Keyboard.GetState();
            if (state.IsKeyDown(Keys.Z))
            {
                biertje.aantalBiertjes = 5;
            }
            if (state.IsKeyDown(Keys.Up))
            {
                up = true;
            }
            else
            {
                up = false;
            }
            if (state.IsKeyDown(Keys.Left))
            {
                if (scrollBackground1.rectangle.X <= -1)
                {
                    scrollBackground1.Links();
                    biertje.BeweegLinks();
                    foreach (ObstakelTest obstakel in obstakelLijst)
                    {
                        obstakel.BeweegLinks();
                    }
                }
                if (speler.playerPosition.X >= 50)
                {
                    speler.moveSpeed = 0.5f;
                    speler.Links();
                    
                }
            }
            if (state.IsKeyDown(Keys.Right))
            {
                if (scrollBackground1.rectangle.X >= -2699)
                {
                    foreach (ObstakelTest obstakel in obstakelLijst)
                    {
                        obstakel.BeweegRechts();
                    }
                    scrollBackground1.Rechts();
                    speler.moveSpeed = 0.5f;
                    biertje.BeweegRechts();
                }
                else
                {
                    speler.moveSpeed = 3.5f;
                }
                if (speler.playerPosition.X <= 600)
                {
                    speler.Rechts();                    
                }
            }
        }
        public void Draw(SpriteBatch spriteBatch)
        {
            foreach (ISprite sprite in allSprites)
            {
                sprite.Draw(spriteBatch);
            }
            if (CheckCollisionsMetGarderobe(spelerRect) && biertje.aantalBiertjes >= 5)
            {
                spriteBatch.DrawString(font, "Druk op Spatie om je jas te pakken.", new Vector2(scrollBackground1.rectangle.X + scrollBackground1.rectangle.Width - 500, 300), Color.White);
            }
        }
        private bool CheckCollisionsMetGarderobe(Rectangle spelerRect)
        {
            Rectangle garderobe = new Rectangle(scrollBackground1.rectangle.X + scrollBackground1.rectangle.Width - 237, 383, 130, 130);
            Rectangle overlap = Rectangle.Intersect(spelerRect, garderobe);
            if (!overlap.IsEmpty && overlap.Width >= spelerRect.Width)
            {
                return true;
            }
            return false;
        }
        private bool CheckEindCondities()
        {
            if (CheckCollisionsMetGarderobe(spelerRect))
            {
                if (state.IsKeyDown(Keys.Space) && biertje.aantalBiertjes >= 5)
                {
                    return true;
                }
            }
            return false;
        }
        private void CheckCollisionsMetBiertjes(Rectangle spelerRect)
        {
            Rectangle bier = new Rectangle((int)biertje.position.X, (int)biertje.position.Y, biertje.texture.Width, biertje.texture.Height);
            Rectangle overlap = Rectangle.Intersect(spelerRect, bier);
            if (!overlap.IsEmpty)
            {
                biertje.Geraakt(scrollBackground1.rectangle.X, scrollBackground1.rectangle.Width);
            }
        }
        public void CheckCollsionsSpelerMetLamp(Rectangle spelerRect)
        {
            foreach (ObstakelTest l in lampLijst)
            {
                if (l.colliderRect != null)
                {
                    foreach (Rectangle r in l.colliderRect)
                    {
                        Rectangle overlap = Rectangle.Intersect(r, spelerRect);
                        if (!overlap.IsEmpty)
                        {
                            biertje.ResetBiertje(scrollBackground1.rectangle.X, scrollBackground1.rectangle.Width);
                            l.ResetVal(scrollBackground1.rectangle.X);
                        }
                    }
                }
            }
        }

        private void CheckCollisionsMetSpeaker(Rectangle spelerRect)
        {
            bovencollision = false;
            foreach (ObstakelTest s in speakerLijst)
            {
                if (s.colliderRect != null)
                {
                    foreach (Rectangle r in s.colliderRect)
                    {
                        Rectangle overlap = Rectangle.Intersect(spelerRect, r);

                        if (!overlap.IsEmpty && overlap.Height < 10 && overlap.Width > 20)
                        {
                            bovencollision = true;
                        }
                    }
                }
            }
        }

        public void CheckColisionDiscobalTrap()
        {
            foreach (ObstakelBal b in discobalLijst)
            {
                Rectangle bal = new Rectangle((int)b.ballPosition.X, (int)b.ballPosition.Y, b.texture.Width / 8, b.texture.Height / 2);
                foreach (ObstakelTest s in speakerLijst)
                {
                    if (s.colliderRect != null)
                    {
                        foreach (Rectangle r in s.colliderRect)
                        {
                            Rectangle overlap = Rectangle.Intersect(r, bal);
                            if (!overlap.IsEmpty)
                            {
                                b.bounce = true;
                                return;
                            }
                            else
                            {
                                b.bounce = false;
                            }
                        }
                    }
                }
            }
        }
        public void CheckCollsionsSpelerMetDiscobal(Rectangle spelerRect)
        {
            foreach (ObstakelBal b in discobalLijst)
            {
                Rectangle bal = new Rectangle((int)b.ballPosition.X, (int)b.ballPosition.Y, b.texture.Width / 8, b.texture.Height / 2); 
                Rectangle collision = Rectangle.Intersect(spelerRect, bal);
                if (!collision.IsEmpty)
                {
                    biertje.ResetBiertje(scrollBackground1.rectangle.X, scrollBackground1.rectangle.Width);
                }
            }
        }

    }
}
